#include "widget.h"
#include "settingsdialog.h"
#include"instructionwidget.h"
#include<qgraphicsview.h>
#include<QTimer>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    // 创建主界面和按钮
    maininterface=new QLabel;
    QPixmap p1=QPixmap("4.png");
    maininterface->setPixmap(p1);
    start = new QPushButton("开始", maininterface);
    instruct = new QPushButton("介绍",maininterface);
    setting = new QPushButton("设置", maininterface);
    title=new QLabel("猫  鼠  跑  酷",maininterface);

    ctom = new QPushButton("Tom", maininterface);
    cjerry = new QPushButton("Jerry", maininterface);

    t=new QLabel(maininterface);
    t->setPixmap(QPixmap("Tom"));
    j=new QLabel(maininterface);
    j->setPixmap(QPixmap("Jerry"));

    t->move(40,110);
    j->move(180,110);


    gamescene=new QGraphicsScene;
    gameview=new QGraphicsView;
    background1=new QGraphicsPixmapItem;
    background2=new QGraphicsPixmapItem;
    tom=new QGraphicsPixmapItem;
    jerry=new QGraphicsPixmapItem;
    temp=new QGraphicsPixmapItem;
    temp=tom;
    gameview->setSceneRect(QRect(0,0,500,280));
    gamescene->setSceneRect(QRect(0,0,500,280));

    background1->setPixmap(p1);
    background2->setPixmap(p1);
    tom->setPixmap(QPixmap("tomfly.png"));
    jerry->setPixmap(QPixmap("jerryfly.png"));
    connect(ctom,SIGNAL(clicked()),this,SLOT(chtom()));
    connect(cjerry,SIGNAL(clicked()),this,SLOT(chjerry()));
    temp->setPos(100,100);
    background2->setPos(500,0);

    gamescene->addItem(background1);
    gamescene->addItem(background2);
    gamescene->addItem(temp);


    gameview->setScene(gamescene);
    gamescene->setParent(this);


    //定时器
    bgm=new QTimer(this);
    bgm->start(100);
    connect(bgm,&QTimer::timeout,this,&Widget::bgmove);


    title->setStyleSheet   ("background-color: transparent; border: none;font-family: 华文彩云; font-size: 40px; color: #F33333;");
    start->setStyleSheet   ("background-color: transparent; border: none;font-family: 华文琥珀; font-size: 24px; color: #F28034;");
    instruct->setStyleSheet("background-color: transparent; border: none;font-family: 华文琥珀; font-size: 24px; color: #F20004;");
    setting->setStyleSheet ("background-color: transparent; border: none;font-family: 华文琥珀; font-size: 24px; color: #00F2DC;");
    ctom->setStyleSheet    ("background-color: blue       ; border: none;font-family: 华文琥珀; font-size: 24px; color: #89FF7C;");
    cjerry->setStyleSheet  ("background-color: brown      ; border: none;font-family: 华文琥珀; font-size: 24px; color: #FF8737;");

    // 设置按钮大小和位置
    title->setFixedSize(250,50);
    start->setFixedSize(80, 30);
    instruct->setFixedSize(80, 30);
    setting->setFixedSize(80, 30);
    ctom->setFixedSize(80, 30);
    cjerry->setFixedSize(80, 30);
    int x = p1.width(), y = p1.height();

    title->move(10,10);
    start->move((x-100), y * 0.25);
    instruct->move((x-100), y * 0.5);
    setting->move((x-100), y*0.75);
    ctom->move(40,230);
    cjerry->move(180,230);

    // 连接设置按钮的点击信号到槽函数
    connect(setting, SIGNAL(clicked()),this,SLOT(showSettingsDialog()));
    connect(instruct,SIGNAL(clicked()),this,SLOT(gotoroleinstruction()));
    connect(start,SIGNAL(clicked()),gameview,SLOT(show()));


    // 设置布局
    QVBoxLayout *mainbox = new QVBoxLayout;
    mainbox->addWidget(maininterface);
    this->setLayout(mainbox);
}
void Widget::gotoroleinstruction()
{
    // 创建并显示 InstructionWidget 对象
    instructionwidget *instructionWidget = new instructionwidget;
    instructionWidget->show();
}

// 槽函数，显示设置对话框
void Widget::showSettingsDialog()
{
    // 创建并显示设置对话框
    SettingsDialog *settingsDialog = new SettingsDialog(this);
    settingsDialog->exec(); // 使用 exec() 函数以模态方式显示对话框
}

Widget::~Widget() {}
